package com.studentscore;

 
import com.studentscore.tools.DataUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 登录窗口
 * @author liyansheng
 * @date 2023/12/27
 */
class LoginWindow extends JFrame implements ActionListener {
    private JTextField userField;
    private JPasswordField passwordField;
    private JButton loginButton, cancelButton;
 
 
    public LoginWindow() {
        setTitle("\u7528\u6237\u767b\u5f55");
 
        // 创建输入框和按钮
        userField = new JTextField(20);
        passwordField = new JPasswordField(20);
        loginButton = new JButton("\u767b\u5f55");
        cancelButton = new JButton("\u53d6\u6d88");
 
        // 添加事件监听器
        loginButton.addActionListener(this);
        cancelButton.addActionListener(this);
 
        // 创建面板并添加组件
        JPanel panel1 = new JPanel(new GridLayout(2, 1, 5, 5));
        panel1.add(new JLabel("\u7528\u6237"));
        panel1.add(new JLabel("\u5bc6\u7801"));
 
        JPanel panel2 = new JPanel(new GridLayout(2, 1, 5, 5));
        panel2.add(userField);
        panel2.add(passwordField);
 
        JPanel panel3 = new JPanel(new FlowLayout());
        panel3.add(loginButton);
        panel3.add(cancelButton);
 
        // 添加组件到窗口
        Container container = getContentPane();
        container.setLayout(new BorderLayout(5, 5));
        container.add(panel1, BorderLayout.WEST);
        container.add(panel2, BorderLayout.CENTER);
        container.add(panel3, BorderLayout.SOUTH);
 
        // 设置窗口属性
        setSize(300, 150);
        setLocationRelativeTo(null);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
 
    // 实现事件监听器接口
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == loginButton) {  // 登录
            String user = userField.getText().trim();
            String pass = new String(passwordField.getPassword()).trim();
            if (user.isEmpty() || pass.isEmpty()) {  // 用户名或密码为空
                JOptionPane.showMessageDialog(this, "\u7528\u6237\u540d\u6216\u5bc6\u7801\u4e0d\u80fd\u4e3a\u7a7a\uff01");
            } else if (isOK(user,pass)) {  // 用户名和密码正确
                dispose();  // 关闭当前窗口
                new IndexWin().setVisible(true);  // 打开表格窗口
            } else {  // 用户名或密码错误
                JOptionPane.showMessageDialog(this, "\u7528\u6237\u540d\u6216\u5bc6\u7801\u9519\u8bef\uff01");
            }
        } else if (e.getSource() == cancelButton) {  // 取消
            System.exit(0);  // 退出程序
        }
    }
 
    public boolean isOK(String user,String password){
        String sql = "select * from user where username=?";
        try {
            ResultSet set = DataUtils.query(sql, new Object[]{user});
            if (set.next()) {
                String name = set.getString(2);
                String pwd = set.getString(3);
                if (user.equals(name) && password.equals(pwd)) {
                    return true;
                }
            } else {
                return false;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return false;
    }
}
 
 